package com.controller.back;

import com.entity.Category;
import com.github.pagehelper.PageInfo;
import com.service.CategoryService;
import com.util.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("back/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @RequestMapping("add")
    public String addCategory(@ModelAttribute("c") @Validated Category category, BindingResult bindingResult, Model model){
        if(ValidatorUtils.handleBindingResult(bindingResult,model)){
            return "forward:/back/category/add-first.jsp";
        }
        categoryService.insertCategory(category);
        return "redirect:/back/category/show.do";
    }
    @RequestMapping("batchRemove")
    public String batchRemoveCategory(@RequestParam("ids") List<Integer> ids){
        for(Integer id:ids){
            categoryService.deleteCategoryById(id);
        }
        return "redirect:/back/category/show.do";
    }
    @RequestMapping("remove")
    public String removeCategory(Integer id, HttpSession session){
        try{
            categoryService.deleteCategoryById(id);
        }catch(Exception e){
            e.printStackTrace();
            session.setAttribute("msg",e.getMessage());
        }

        return "redirect:/back/category/show.do";
    }
    @RequestMapping("show")
    public String showAllCategory(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "3") Integer pageSize,
                                  Model model){
        PageInfo<Category> categories = categoryService.selectPageCategory(pageNum,pageSize);
        model.addAttribute("pageInfo",categories);
        return "forward:/back/category/show.jsp";
    }
    @RequestMapping("showOne")
    public String showOneCategory(Integer id,HttpServletRequest req){
        Category category = categoryService.selectCategoryById(id);
        req.setAttribute("category",category);
        return "forward:/update";
    }
    @RequestMapping("update")
    public String updateCategory(@Validated Category category, BindingResult bindingResult, Model model){
        if(ValidatorUtils.handleBindingResult(bindingResult,model)){
            return "forward:/update.jsp";
        }
        categoryService.updateCategory(category);
        return "forward:/back/category/show.do";
    }
    @RequestMapping("showLevel1Category")
    public String showLevel1Category(Model model){
        List<Category> categories = categoryService.selectCategoriesByLevels(1);
        model.addAttribute("categories",categories);
        return "forward:/back/category/add-second.jsp";
    }

    @RequestMapping("showLevel2Category")
    public String showLevel2Category(Model model){
        List<Category> categories = categoryService.selectCategoriesByLevels(2);
        model.addAttribute("categories",categories);
        return "forward:/back/book/add.jsp";
    }
}
